package com.fubowen.lepton.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import androidx.room.Upsert
import com.fubowen.lepton.bean.Setting
import com.fubowen.reader.db.bean.SettingBean
import kotlinx.coroutines.flow.Flow

@Dao
interface SettingDao {
    @Query("select * from SettingBean")
    fun all(): List<SettingBean>

    fun map(): Map<String, String?> {
        return all().associate { it.key to it.value }
    }

    @Query("select * from SettingBean")
    fun allFlow(): Flow<List<SettingBean>>

    @Query("select * from SettingBean where Id = :id")
    fun get(id: Int): SettingBean?

    @Query("select value from SettingBean where `key` = :key")
    fun get(key: String): String?

    @Query("select value from SettingBean where `key` = :key")
    fun getFlow(key: String): Flow<String?>

    @Query("select * from SettingBean where nameSpace = :nameSpace")
    fun nameSpaceFlow(nameSpace: String): Flow<List<SettingBean>>

    @Insert
    fun set(bean: SettingBean)

    @Query("INSERT INTO SettingBean(`key`,value)values(:key,:value)")
    fun set(key: String, value: String?)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun setOrPut(bean: SettingBean)

    @Update
    fun put(bean: SettingBean)

    @Delete
    fun del(bean: SettingBean)

    @Query("DELETE FROM SettingBean")
    fun clear()

    @Query("REPLACE INTO SettingBean(`key`, value) VALUES (:key,:value)")
    fun replace(key: String, value: String?)
}